﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Candy.Infrastructure;
using Candy.Web;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Candy.Infrastructure;
using Candy.Model;

namespace Candy.ViewModel
{
    public abstract class AuthRequiredViewModelBase : CandyViewModelBase
    {
        private AuthUser _user;
        public AuthUser User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;
                RaisePropertyChanged("User");
            }
        }


        public bool IsLoggedIn
        {
            get
            {
                if (IsInDesignMode)
                    return true;
                else
                    return WebContext.Current.Authentication.User != null && WebContext.Current.Authentication.User.Identity.IsAuthenticated;
            }
            set
            {

                RaisePropertyChanged("IsLoggedIn");
            }
        }


        INavigationService _navigationService;
        protected IUserManager _userManger;
        public AuthRequiredViewModelBase(INavigationService navigationService, IUserManager usermanger)
        {
            _navigationService = navigationService;
            _userManger = usermanger;

            if (!IsInDesignMode)
            {
                _userManger.LoggedIn += _userManger_LoggedIn;
                _userManger.LoggedOut += _userManger_LoggedOut;
                //WebContext.Current.Authentication.LoggedIn += Authentication_LoggedIn;
                //WebContext.Current.Authentication.LoggedOut += Authentication_LoggedOut;
            }
        }

        void _userManger_LoggedOut(object sender, EventArgs e)
        {
            User = null;
            IsLoggedIn = false;
            _navigationService.ShowLogin();
        }

        void _userManger_LoggedIn(object sender, EventArgs e)
        {
            User = _userManger.AuthenticatedUser;
            IsLoggedIn = true;
            OnLoggedIn();
        }


        //void Authentication_LoggedOut(object sender, System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs e)
        //{
        //    User = null;
        //    IsLoggedIn = false;
        //    _navigationService.ShowLogin();

        //}

        //void Authentication_LoggedIn(object sender, System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs e)
        //{
        //    User = (AuthUser)e.User;
        //    IsLoggedIn = true;
        //    OnLoggedIn();
        //}

        protected bool CheckAuthentication()
        {
            if (IsLoggedIn)
            {
                return true;
            }
            else
            {
                _navigationService.ShowLogin();
                return false;
            }
        }

        protected abstract void OnLoggedIn();
        protected abstract void OnLoggedOut();
    }
}
